﻿using CoreShop.Model;
using CoreShop.Services;
using gregn6Lib;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoreShop.App.Units.Helper
{
    public class GridReportHelper
    {
        //定义Grid++Report报表主对象
        GridppReport Report = new GridppReport();

        /// <summary>
        /// 自动导入下载的报表文档
        /// </summary>
        public void ImportGridReport(UIForm uIForm)
        {
            string sFilePath = System.Windows.Forms.Application.StartupPath + "\\report";
            FolderBrowserDialog fbd_report = new FolderBrowserDialog();
            fbd_report.SelectedPath = sFilePath;
            string[] sReportFiles = Directory.GetFiles(fbd_report.SelectedPath, "*.grf");
            if (sReportFiles.Length > 0)
            {
                try
                {
                    foreach (string file in sReportFiles)
                    {
                        ImportRptByFile(uIForm, file);
                        //string reportFile = Path.GetFileName(file);
                        //var billtemplate = billtemplateList.Find(p=>p.reportFile == reportFile);
                        //if (billtemplate != null)
                        //{
                        //    Report = new GridppReport();
                        //    Report.LoadFromFile(file);
                        //    string reportName = Report.Title;
                        //    billtemplate.reportData = Report.SaveToStr();
                        //}
                    }                 
                }
                catch { }
            }
        }

        /// <summary>
        /// 导入下载的报表文档
        /// </summary>
        public bool ImportRptByFile(UIForm uIForm, string file, int billtypeId = 0)
        {
            try
            {
                Report = new GridppReport();
                Report.LoadFromFile(file);
                string billtemplateName = Report.Title;
                string reportFileName = Path.GetFileName(file);
                var billtemplateSrv = new corecmsprintbilltemplateServices();
                var billtemplateList = billtemplateSrv.AsQueryable().Where(p => p.reportFile == reportFileName || p.name == billtemplateName).ToList();
                if (billtemplateList.Count > 0)
                {
                    if (uIForm.ShowAskDialog("提示：", $"小票模板<{billtemplateName}>已经存在，要替换吗？", UIStyle.Red))
                    {
                        var billtemplate = billtemplateList.First();
                        billtemplate.reportData = Report.SaveToStr();
                        billtemplateSrv.InsertOrUpdate(billtemplate);
                    } 
                    else 
                        return false;
                }
                else
                if (billtypeId > 0)
                {
                    var billtemplate = new corecmsprintbilltemplate();
                    billtemplate.name = Report.Title;
                    billtemplate.billtypeId = billtypeId;
                    billtemplate.reportFile = reportFileName;
                    billtemplate.reportData = Report.SaveToStr();
                    billtemplateSrv.InsertOrUpdate(billtemplate);
                }
                if (billtypeId == 0)
                    if (File.Exists(file)) File.Delete(file);      
                return true;
            }
            catch {
                return false;
            }            
        }

    }
}
